/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.dto;

import cz.muni.fi.heathrow.entities.Aircraft;
import cz.muni.fi.heathrow.entities.Destination;
import cz.muni.fi.heathrow.entities.Flight;
import cz.muni.fi.heathrow.entities.Steward;
import cz.muni.fi.heathrow.entities.User;
import cz.muni.fi.heathrowapi.dto.AircraftDTO;
import cz.muni.fi.heathrowapi.dto.DestinationDTO;
import cz.muni.fi.heathrowapi.dto.FlightDTO;
import cz.muni.fi.heathrowapi.dto.StewardDTO;
import cz.muni.fi.heathrowapi.dto.UserDTO;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.Transformer;

/**
 *
 * @author Ufonzak
 */
public class DTOAssembler
{

    private DTOAssembler() {
    }

    
    public static Transformer gegFlightCollectionTransorm()
    {
        return new Transformer()
        {

            @Override
            public Object transform(Object o)
            {
                return DTOAssembler.CreateFlightDTO((Flight) o);
            }
        };
    }

    public static List<DestinationDTO> createDestinationDTOList(List<Destination> destinations) {
        List<DestinationDTO> dtoDest = new ArrayList<>();

        for(Destination d : destinations){
            dtoDest.add(CreateDestinationDTO(d));
        }
        return dtoDest;
    }
    
    public static List<AircraftDTO> createAircraftDTOList(List<Aircraft> aircraft) {
        List<AircraftDTO> dtoAir = new ArrayList<>();

        for(Aircraft ac : aircraft){
            dtoAir.add(CreateAircraftDTO(ac));
        }
        return dtoAir;
    }
    
    public static List<UserDTO> createUserDTOList(List<User> users) {
        List<UserDTO> dtoUser = new ArrayList<>();

        for (User u : users) {
            dtoUser.add(CreateUserDTO(u));
        }
        return dtoUser;
    }
    
    public static AircraftDTO CreateAircraftDTO(Aircraft domainObject)
    {
        AircraftDTO dtoObject = new AircraftDTO();

        UpdateAircraftDTO(dtoObject, domainObject);

        return dtoObject;
    }

    public static void UpdateAircraftDTO(AircraftDTO dtoObject, Aircraft domainObject)
    {
        dtoObject.setId(domainObject.getId());
        dtoObject.setCode(domainObject.getCode());
        dtoObject.setType(domainObject.getType());
        dtoObject.setCapacity(domainObject.getCapacity());
    }

    public static void UpdateAircraftDomainObject(AircraftDTO dtoObject, Aircraft domainObject)
    {
        //do not update id
        domainObject.setType(dtoObject.getType());
        domainObject.setCapacity(dtoObject.getCapacity());
        domainObject.setCode(dtoObject.getCode());
    }

    public static StewardDTO CreateStewardDTO(Steward domainObject)
    {
        StewardDTO dtoObject = new StewardDTO();
        UpdateStewardDTO(dtoObject, domainObject);

        return dtoObject;
    }

    public static void UpdateDestinationDTO(DestinationDTO dtoObject, Destination domainObject)
    {
        dtoObject.setId(domainObject.getId());
        dtoObject.setCity(domainObject.getCity());
        dtoObject.setCode(domainObject.getCode());
        dtoObject.setState(domainObject.getState());
        dtoObject.setStateCode(domainObject.getStateCode());
    }

    public static void UpdateStewardDTO(StewardDTO dtoObject, Steward domainObject)
    {
        dtoObject.setFirstName(domainObject.getFirstName());
        dtoObject.setLastName(domainObject.getLastName());
        dtoObject.setId(domainObject.getId());
        dtoObject.setPersonalId(domainObject.getPersonalId());
    }

    public static void UpdateStewardDomainObject(StewardDTO dtoObject, Steward domainObject)
    {
        //do not update id
        domainObject.setFirstName(dtoObject.getFirstName());
        domainObject.setLastName(dtoObject.getLastName());
        domainObject.setPersonalId(dtoObject.getPersonalId());
    }

    public static List<StewardDTO> CreateStewardDTOList(List<Steward> stewards){
        List<StewardDTO> stewardsDTO = new ArrayList<>();
        
        for(Steward stew : stewards){
            stewardsDTO.add(CreateStewardDTO(stew));
        }
        return stewardsDTO;
    }
    public static DestinationDTO CreateDestinationDTO(Destination domainObject)
    {
        DestinationDTO dtoObject = new DestinationDTO();

        UpdateDestinationDTO(dtoObject, domainObject);

        return dtoObject;
    }

    public static void UpdateDestinationDomainObject(DestinationDTO dtoObject, Destination domainObject)
    {
        domainObject.setCity(dtoObject.getCity());
        domainObject.setCode(dtoObject.getCode());
        domainObject.setState(dtoObject.getState());
        domainObject.setStateCode(dtoObject.getStateCode());
    }

    public static FlightDTO CreateFlightDTO(Flight domainObject)
    {
        if (domainObject == null)
            return null;
        
        FlightDTO dtoObject = new FlightDTO();

        UpdateFlightDTO(dtoObject, domainObject);

        return dtoObject;
    }

    public static void UpdateFlightDomainObject(FlightDTO dtoObject, Flight domainObject)
    {
        //update vazeb zajistí service

        domainObject.setArrival(dtoObject.getArrival());
        domainObject.setDeparture(dtoObject.getDeparture());
    }

    public static void UpdateFlightDTO(FlightDTO dtoObject, Flight domainObject)
    {
        dtoObject.setId(domainObject.getId());

        if (domainObject.getAircraft() != null)
            dtoObject.setAircraft(CreateAircraftDTO(domainObject.getAircraft()));

        dtoObject.setArrival(domainObject.getArrival());
        dtoObject.setDeparture(domainObject.getDeparture());

        if (domainObject.getFlightFrom() != null)
            dtoObject.setFlightFrom(CreateDestinationDTO(domainObject.getFlightFrom()));
        if (domainObject.getFlightTo() != null)
            dtoObject.setFlightTo(CreateDestinationDTO(domainObject.getFlightTo()));

        List<StewardDTO> stews = new ArrayList<>();

        if (domainObject.getStewards() != null)
            for (Steward st : domainObject.getStewards())
                stews.add(CreateStewardDTO(st));

        dtoObject.setStewards(stews);
    }
    
    public static UserDTO CreateUserDTO(User domainObject) {
        UserDTO dtoObject = new UserDTO();

        UpdateUserDTO(dtoObject, domainObject);

        return dtoObject;
    }
    
    public static void UpdateUserDTO(UserDTO dtoObject, User domainObject) {
        dtoObject.setId(domainObject.getId());
        dtoObject.setUsername(domainObject.getUsername());
        dtoObject.setRoles(domainObject.getRoles());
    }
    
    public static void UpdateUserDomainObject(UserDTO dtoObject, User domainObject, boolean create) {
        //do not update id
        if (create)
        {
            domainObject.setUsername(dtoObject.getUsername());
        }
        
        domainObject.setRoles(dtoObject.getRoles());
    }
}
